<?php
namespace Index\Controller;
use Think\Controller;
class JieController extends Controller {
    public function index(){

    	$id=I('get.id');
    	$sql = "select t1.*,t2.face,t2.nickname  from question t1 left join user t2 on t1.uid=t2.id where t1.id='$id'";
    	
    	$data=M()->query($sql);

    	$hotview=D('question')->hotview();

    	$hotanswer=D('question')->hotanswer();

    	$this->assign('hotview',$hotview);
    	var_dump($hotanswer);
    	$this->assign('hotanswer',$hotanswer);

    	$this->assign('ques',current($data));
       $this->display();

   }

   //点发布跳转到这方法
   	public function add()
   	{
   		$cate = M('category')->select();

   		// var_dump($cate);
   		$tempArr=[];
   		foreach ($cate as $row) {
   			if ($row['fid']==0) {
   				$tempArr[$row['id']]=$row;
   			}
   		}

   		foreach ($cate as $son) {
   			foreach ($tempArr as $key => $fater) {
   				if ($son['fid']==$key) {
   					$tempArr[$key]['son'][]=$son;
   				}
   			}
   		}
		// var_dump($tempArr);

   		$this->assign('cate',$tempArr);
   		//随机获取一条问题
    	$question = D('vercode')->getOne();
        //分配变量
    	$this->assign('question',$question);

   		$this->display();
   	}
   	//异步传进这方法
   	public function addjie()
   	{

   			D('vercode')->checkCode(I('post.vercode'));

   			$data = I('post.');
	    	$data['uid']= $_SESSION['uid'];
	    	$data['create_time']=time();

   			if ($id=M('question')->add($data)) {
   				$url=U('index/jie/index',array('id'=>$id));
   				// var_dump($url);
   				Reajax(0,array('info'=>"发布成功",'url'=>$url));
   			}else{
   				Reajax(1,"发布失败");
   			}

   	}

   	public function upload()
   	{
   				$upload = new \Think\Upload();// 实例化上传类
			    $upload->maxSize   =     3145728 ;// 设置附件上传大小
			    $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
			    $upload->rootPath  =     './Uploads/jie/'; // 设置附件上传根目录
			    $upload->savePath  =     ''; // 设置附件上传（子）目录
			    // 上传文件 
			    $info   =   $upload->upload();
			   
			    if(!$info) {// 上传错误提示错误信息
			        $this->error($upload->getError());
			    }else{// 上传成功
			       
			    	$info=current($info);

			        $img=__ROOT__."/Uploads/jie/".$info['savepath'].$info['savename'];
			        $arr=array(
			        	'code'=>0,
			        	'msg'=>"成功",
			        	'data'=>array(
			        		'src'=>$img,
			       		 )
	        		);
	        		echo json_encode($arr);
	    		}
   	}



}

?>